﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Collector.Analyzer
{
    class IAnalyzer
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct PulseResult
        {
            public int lengths;
            public IntPtr data;

            // 手动转换 UTF-8 字节为字符串（.NET 4.0 没有 PtrToStringUTF8）
            public string Data
            {
                get
                {
                    if (data == IntPtr.Zero) return null;
                    // 计算字符串长度（直到遇到 '\0'）
                    //int length = 0;
                    //while (Marshal.ReadByte(data, length) != 0) length++;
                    byte[] buffer = new byte[lengths];
                    Marshal.Copy(data, buffer, 0, lengths);
                    return Encoding.UTF8.GetString(buffer);
                }
            }
        }

        [DllImport("libPulseCoreSabre.dll", CharSet = CharSet.Ansi, EntryPoint = "create_handler", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr create_handler(int num, int baseline_cun, int baseline_guan, int baseline_chi, int type);

        [DllImport("libPulseCoreSabre.dll", CharSet = CharSet.Ansi, EntryPoint = "fill_frame_package", CallingConvention = CallingConvention.Cdecl)]
        public static extern int fill_frame_package(IntPtr handler, byte[] frames, int framesize, int framechunk, int index);

        [DllImport("libPulseCoreSabre.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr analyzing_data(IntPtr handler, StringBuilder result, int result_length);

        [DllImport("libPulseCoreSabre.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void free_pulse_result(IntPtr data);

        [DllImport("libPulseCoreSabre.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool release_handler(IntPtr handler);

        [DllImport("PulseInterface.dll", EntryPoint = "AnalysePulseWithMultichannelOut",
            CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]//type 0表示分段采集，1表示连续采集
        public static extern void AnalysePulseWithMultichannelOut(ref WaveParamStc waveParam, byte[] data, int[] bytelen, StringBuilder jres, int type);

    }
}
